<?php

    session_start();

    $error = 0;
    $errormessage = '';
    if (isset($_GET['error'])) {
        $error = $_GET['error'];
    }
    if ($error != '0') {
        $error = 1;
        $errormessage = 'test error message';
    }

    $data = '';

    if (!empty($_SESSION['user_id'])) {
        $user_id = $_SESSION['user_id'];
        if (isset($_GET['rating'])) {
            $rating = $_GET['rating'];
            if (isset($_GET['photoid'])) {
                $photoid = $_GET['photoid'];
                require('connect.php');
                $query_get = "select * from rating where userid='$user_id' and photoid='$photoid'";
                    $rows = array();
                $res = mysql_query($query_get) or die(mysql_error());
                while($r = mysql_fetch_assoc($res)) {
                    $rows[] = $r;
                }
                if (count($rows) == 0) {
                    $query_set = "INSERT INTO rating (userid, photoid, rating)
                      VALUES ('$user_id', '$photoid', '$rating')";
                    $sql = mysql_query($query_set) or die(mysql_error());
                    mysql_close($connection);
                    $data = 'ok';
                } else {
                    $errormessage = 'you was rated this photo';
                    $data = 'already rated';
                }
            } else {
                $error = 1;
                $errormessage = 'please pass photoid';
            }
        } else {
            $error = 1;
            $errormessage = 'please pass rating';
        }
    } else {
        $error = 1;
        $errormessage = 'user is not logged';
    }
    $ajaxresult = (object) array('error' => $error, 'errormessage' => $errormessage, 'data' => $data);
    echo json_encode($ajaxresult);
?>